All files / src/services epg.ts

0% Statements 0/13
100% Branches 0/0
0% Functions 0/8
0% Lines 0/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                                                                                                                                                                                   
import { apiService } from './api';
import { API_ENDPOINTS } from '@/constants/api';
import { ApiResult } from '@/types/api';
 
export interface EpgSource {
  id: number;
  name: string;
  url: string;
  timezone: string;
  cache_ttl_minutes: number;
  is_active: boolean;
}
 
export interface CreateEpgSourceRequest {
  name: string;
  url: string;
  timezone?: string;
  cache_ttl_minutes?: number;
  is_active?: boolean;
}
 
export interface UpdateEpgSourceRequest {
  name?: string;
  url?: string;
  timezone?: string;
  cache_ttl_minutes?: number;
  is_active?: boolean;
}
 
export interface EpgChannelInfo {
  id: string;
  display_name: string;
  icon?: string;
}
 
export interface EpgProgramsResponse {
  programs: Array<{
    channel_id: string;
    title: string;
    description?: string;
    start: string; // ISO8601 UTC
    end: string;   // ISO8601 UTC
    category?: string;
  }>;
}
 
class EpgService {
  // Admin sources
  async getSources(): Promise<ApiResult<EpgSource[]>> {
    return apiService.get<EpgSource[]>(API_ENDPOINTS.ADMIN.EPG.SOURCES);
  }
 
  async createSource(payload: CreateEpgSourceRequest): Promise<ApiResult<EpgSource>> {
    return apiService.post<EpgSource>(API_ENDPOINTS.ADMIN.EPG.SOURCES, payload);
  }
 
  async updateSource(id: number, payload: UpdateEpgSourceRequest): Promise<ApiResult<EpgSource>> {
    return apiService.put<EpgSource>(API_ENDPOINTS.ADMIN.EPG.SOURCE_BY_ID(id), payload);
    }
 
  async deleteSource(id: number): Promise<ApiResult<{ success: boolean }>> {
    return apiService.delete<{ success: boolean }>(API_ENDPOINTS.ADMIN.EPG.SOURCE_BY_ID(id));
  }
 
  async discoverChannels(id: number): Promise<ApiResult<EpgChannelInfo[]>> {
    return apiService.get<EpgChannelInfo[]>(API_ENDPOINTS.ADMIN.EPG.SOURCE_CHANNELS(id));
  }
 
  async patchTvChannelEpg(channelId: number, payload: { epg_source_id?: number; epg_channel_id?: string; epg_offset_minutes?: number }): Promise<ApiResult<{ success: boolean }>> {
    return apiService.patch<{ success: boolean }>(API_ENDPOINTS.ADMIN.EPG.TV_CHANNEL_EPG(channelId), payload);
  }
 
  async getTvChannelEpg(channelId: number): Promise<ApiResult<{ epg_source_id?: number; epg_channel_id?: string; epg_offset_minutes: number }>> {
    return apiService.get<{ epg_source_id?: number; epg_channel_id?: string; epg_offset_minutes: number }>(API_ENDPOINTS.ADMIN.EPG.TV_CHANNEL_EPG(channelId));
  }
 
  // Delivery
  async getEpg(params: { channel_ids: number[]; start: string; end: string }): Promise<ApiResult<EpgProgramsResponse>> {
    const qs = new URLSearchParams({
      channel_ids: params.channel_ids.join(','),
      start: params.start,
      end: params.end}).toString();
    return apiService.get<EpgProgramsResponse>(`${API_ENDPOINTS.TV.EPG}?${qs}`);
  }
}
 
export const epgService = new EpgService();
export type { EpgService };